<?php

include(dirname(__FILE__) . '/../../bootstrap/Doctrine.php');

$t = new lime_test(1);

$t->comment('->preInsert()');
$new_user = createUser('user_1', 'trader');
$new_user->save();

$trader_profile = Doctrine_Core::getTable('TraderProfile')->findOneBy('user_id', $new_user->getId());
$new_trader_profile = createTraderProfil(array('user_id' => $new_user->getId()));
$new_trader_profile->save();

$t->isnt($trader_profile->get('id'), $new_trader_profile->get('id'), '->preInsert() remove correctly the default row created by sfGuarUser::postInsert() and add a new one');

function createTraderProfil($defaults = array())
{
  $trader_profile = new TraderProfile();
  $trader_profile->fromArray(array_merge(
                  array(
                      'loss' => 10000,
                      'sex' => 'H'
                  ), $defaults));

  return $trader_profile;
}

function createUser($name, $group, $defaults = array())
{
  $user = new sfGuardUser();
  $user->setGroups(Doctrine::getTable('sfGuardGroup')->findBy('name', $group));

  $user->fromArray(array_merge(
                  array(
                      'first_name' => $name,
                      'last_name' => $name,
                      'email_address' => $name.'@example.com',
                      'username' => $name,
                      'password' => 'secrets'
                  ), $defaults));

  return $user;
}